<template>
    <div class="login-container">
        <div class="login-form-wrapper animate-fade-in">
            <h2 class="login-title">爱吃兔溯源系统</h2>
            <el-form ref="loginForm" :model="UserData" :rules="rules" class="login-form">
                <el-form-item label="用户名" prop="UserName" :validate-event="false">
                    <el-input v-model="UserData.UserName" placeholder="请输入用户名" prefix-icon="User"
                        @keyup.enter="handleLogin" :class="{ 'input-error': formErrors.UserName }" />
                    <div v-if="formErrors.UserName" class="error-message">{{ formErrors.UserName }}</div>
                </el-form-item>
                <el-form-item label="密码" prop="UserPwd" :validate-event="false">
                    <el-input v-model="UserData.UserPwd" show-password placeholder="请输入密码" prefix-icon="Lock"
                        @keyup.enter="handleLogin" :class="{ 'input-error': formErrors.UserPwd }" />
                    <div v-if="formErrors.UserPwd" class="error-message">{{ formErrors.UserPwd }}</div>
                </el-form-item>
                <el-form-item>
                    <div class="login-options">
                        <el-switch v-model="UserData.Isremember" inline-prompt
                            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text="记住密码"
                            inactive-text="忘记密码" @change="handleSwitchChange" />
                        <el-button v-if="UserData.Isremember" type="text" size="small" class="forgot-password-btn"
                            @click="handleForgotPassword">
                            忘记密码?
                        </el-button>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-button" :loading="isLoading" @click="handleLogin">
                        <span v-if="!isLoading">登录</span>
                        <span v-else>登录中...</span>
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
const pinia = useCounterStore();  //持久化数据
const router = useRouter();
const isLoading = ref(false);
// 表单数据
const UserData = reactive({
    UserName: "",
    UserPwd: "",
    Isremember: false
})
// 表单错误信息
const formErrors = reactive({
    UserName: '',
    UserPwd: ''
})
// 表单验证规则
const rules = computed(() => ({
    UserName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符之间', trigger: 'blur' }
    ],
    UserPwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 2, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
    ]
}))
// 页面加载时反填密码
onMounted(() => {
    Outway();
})
// 反填密码功能
const Outway = () => {
    // 修复赋值错误：使用 === 而不是 = 
    if (pinia.UserInfo.IsRember === true) {
        UserData.UserName = pinia.UserInfo.UserName || '';
        UserData.UserPwd = pinia.UserInfo.UserPwd || '';
        UserData.Isremember = pinia.UserInfo.IsRember;
    }
}
// 处理开关状态变化
const handleSwitchChange = (value: boolean) => {
    if (value === false) {
        // 当切换到"忘记密码"时，提供忘记密码功能
        // 这里可以显示忘记密码的弹窗或跳转到忘记密码页面
    }
}
// 忘记密码功能
const handleForgotPassword = () => {
    ElMessageBox.alert(
        '请联系系统管理员重置密码。\n管理员邮箱：admin@example.com',
        '忘记密码',
        {
            confirmButtonText: '确定',
            type: 'info'
        }
    );
}
// 登录功能
const handleLogin = async () => {
    // 设置加载状态
    isLoading.value = true;
    try {
        const res = await axios.get('/api/Goods/Login', {
            params: {
                UserName: UserData.UserName,
                UserPwd: UserData.UserPwd,
            }
        });
        if (res.data.Num == 200) {
            // 处理记住密码
            if (UserData.Isremember === true) {
                pinia.UserInfo.IsRember = true;
                pinia.UserInfo.UserName = UserData.UserName;
                pinia.UserInfo.UserPwd = UserData.UserPwd;
            } else {
                pinia.UserInfo.IsRember = false;
                pinia.UserInfo.UserName = "";
                pinia.UserInfo.UserPwd = "";
            }
            ElMessage.success(res.data.Message);
            console.log(res.data);
            pinia.UserInfo.UserName = res.data.Data.UserName;
            pinia.UserInfo.RealName = res.data.Data.RealName;
            pinia.UserInfo.AccountToken = res.data.Data.AccountToken;
            // pinia.UserInfo.LongToken = res.data.Data.LongToken;
            console.log(pinia.UserInfo);
            // 添加成功动画效果
            const loginFormWrapper = document.querySelector('.login-form-wrapper');
            if (loginFormWrapper) {
                loginFormWrapper.classList.add('animate-success');
                // 延迟跳转，让用户看到成功动画
                setTimeout(() => {
                    router.push('/Main');
                }, 500);
            } else {
                router.push('/Main');
            }
        } else {
            ElMessage.error(res.data.Message);
        }
    } catch (error) {
        ElMessage.error('登录失败，请检查网络连接或稍后再试');
        console.error('Login error:', error);
    } finally {
        // 重置加载状态
        isLoading.value = false;
    }
}
</script>
<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #e6f7ff;
    position: relative;
    overflow: hidden;
}

/* 背景装饰元素 */
.login-container::before {
    content: '';
    position: absolute;
    top: -10%;
    left: -10%;
    width: 120%;
    height: 120%;
    background: radial-gradient(circle at 10% 20%, rgba(64, 158, 255, 0.05) 0%, rgba(64, 158, 255, 0) 20%),
        radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.05) 0%, rgba(64, 158, 255, 0) 20%);
    z-index: 0;
}

.login-form-wrapper {
    width: 400px;
    padding: 40px;
    background-color: #ffffff;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 1;
    transition: all 0.3s ease;
}

/* 表单容器悬停效果 */
.login-form-wrapper:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

/* 动画效果 */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes successShake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-10px);
    }

    75% {
        transform: translateX(10px);
    }
}

.animate-fade-in {
    animation: fadeIn 0.6s ease-out;
}

.animate-success {
    animation: successShake 0.5s ease-in-out;
}

/* Logo 样式 */
.logo-container {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
}

.system-logo {
    width: 80px;
    height: 80px;
    transition: transform 0.3s ease;
}

.system-logo:hover {
    transform: rotate(180deg);
}

.login-title {
    text-align: center;
    margin-bottom: 30px;
    color: #303133;
    font-size: 24px;
    font-weight: 600;
}

.login-form {
    width: 100%;
}

/* 登录选项样式 */
.login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.forgot-password-btn {
    color: #409eff;
    font-size: 12px;
    transition: color 0.3s ease;
}

.forgot-password-btn:hover {
    color: #66b1ff;
}

.login-button {
    width: 100%;
    height: 40px;
    font-size: 16px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.login-button:not(:disabled):hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.el-form-item {
    margin-bottom: 24px;
    position: relative;
}

/* 错误信息样式 */
.error-message {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
}

/* 输入框错误状态样式 */
.input-error .el-input__wrapper {
    border-color: #f56c6c !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .login-form-wrapper {
        width: 90%;
        max-width: 400px;
        padding: 30px 20px;
        margin: 20px;
    }

    .system-logo {
        width: 60px;
        height: 60px;
    }

    .login-title {
        font-size: 20px;
        margin-bottom: 20px;
    }
}

/* 输入框聚焦动画 */
.el-input__wrapper:focus-within {
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}
</style>